package Day2025_04_02;

import java.util.ArrayList;
import java.util.List;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class LeetCode257 {
    List<String> list;
    public List<String> binaryTreePaths(TreeNode root) {
        list = new ArrayList<>();
        dfs (root, new StringBuffer());
        return list;
    }
    private void dfs (TreeNode root, StringBuffer _ret) {
        StringBuffer ret = new StringBuffer(_ret);
        ret.append(root.val);
        if (root.left == null && root.right == null) {
            list.add(ret.toString());
            return;
        }
        ret.append("->");
        if (root.left != null) dfs(root.left, ret);
        if (root.right != null) dfs(root.right, ret);
    }
}
